import { Component,OnInit,ComponentFactoryResolver} from '@angular/core';
import { HttpClient } from '@angular/common/http';

import {ActivatedRoute} from "@angular/router";
import {HttpURLService} from "../../../httpURL.service";

/**
 * 数采仪反控服务
 * guopeng
 * 2017.11.27
 */
@Component({
  selector: 'app-collectData',
  templateUrl: './collectData.component.html',
  styleUrls: ['./collectData.component.css']
})
export class CollectDataComponent implements OnInit{
  DemoService: any;
  public url:string = this.httpURLService.getUrl("communication");
  public message :ReverseMessage;
  public dacId: any;
  public ReverseLogId : any;
  public countDown: any;
  private list:Array<any>;
  private time : any = 10;
  private Interval :any; //定时器
  private inputList : string;

  constructor(private http: HttpClient,
    private cfr:ComponentFactoryResolver,private httpURLService: HttpURLService,public activeRoute: ActivatedRoute
  ){}

	onstructor(){ }
	ngOnInit(){
    this.activeRoute.queryParams.subscribe(params => {
      this.dacId = params['id'];
    });
	}
  cleanDiv(){
    $('.buildDiv').html("");
    // $('input:radio[name="CN"]').attr('checked','');
    // console.log($('input:radio[name="CN"]:checked').val());
  }
  buildDiv(code){
    $('.buildDiv').html("");
    var input = "";
    if(code=="1000"){
      input ='<div class="row"><div class="form-group col-md-6">\n' +
        '  <label>超时时间：</label>\n' +
        '  <input type="text" class="form-control" id="timeout" style="background-color: rgba(72,172,194,0.8);border: 1px solid #41a1b8;" >\n' +
        '</div>\n' +
        '<div class="form-group col-md-6">\n' +
        '  <label>重发次数：</label>\n' +
        '  <input type="text" class="form-control" id="retransmission" style="background-color: rgba(72,172,194,0.8);border: 1px solid #41a1b8;" >\n' +
        '</div></div>';
    }else if(code=="1012"){
      input = '<div class="row"><div class="form-group col-md-6"><label>污染物编码：</label>\n' +
        '<input type="text" id="pollutantCode" class="form-control" style="background-color: rgba(72,172,194,0.8);border: 1px solid #41a1b8;" >\n' +
        '</div>' +
        '<div class="form-group col-md-6 col-sm-6 col-xs-6">\n' +
        '  <label>上位机系统时间：</label>\n' +
        '  <input type="text" class="form-control" id="startTime" style="background-color: rgba(72,172,194,0.8);border: 1px solid #41a1b8;">\n' +
        '</div></div>';
    }else if(code=="1062"){
      input = '<div class="row"><div class="form-group col-md-6"><label>实时数据间隔：</label>\n' +
        '<input type="text" id="realTimeData" class="form-control" style="background-color: rgba(72,172,194,0.8);border: 1px solid #41a1b8;" >\n' +
        '</div></div>';
    }else if(code=="1064"){
      input = '<div class="row"><div class="form-group col-md-6"><label>分钟数据间隔：</label>\n' +
        '<input type="text" id="minuteData" class="form-control" style="background-color: rgba(72,172,194,0.8);border: 1px solid #41a1b8;" >\n' +
        '</div></div>';
    }else if(code=="1072"){
      input = '<div class="row"><div class="form-group col-md-6"><label>密码：</label>\n' +
        '<input type="text" id="password" class="form-control" style="background-color: rgba(72,172,194,0.8);border: 1px solid #41a1b8;" >\n' +
        '</div></div>';
    }else if((code=="2031")||(code=="2041")||(code=="2051")||(code=="2061")){
      input = '<div class="row"><div class="form-group col-md-6"><label>起始时间：</label>\n' +
        '<input type="text" class="form-control" id="startTime" style="background-color: rgba(72,172,194,0.8);border: 1px solid #41a1b8;" >\n' +
        '</div><div class="form-group col-md-5"><label>截止时间：</label>\n' +
        '<input id="endTime" type="text" class="form-control" style="background-color: rgba(72,172,194,0.8);border: 1px solid #41a1b8;">\n' +
        '</div></div>';
    }else if((code=="1011")||(code=="3011")||(code=="3012")||(code=="3013")||(code=="3014")||(code=="3017")||(code=="3018")||(code=="3019")||(code=="4003")){
      input = '<div class="row"><div class="form-group col-md-6"><label>污染物编码：</label>\n' +
        '<input type="text" id="pollutantCode" class="form-control" style="background-color: rgba(72,172,194,0.8);border: 1px solid #41a1b8;" >\n' +
        '</div></div>';
    }else if(code=="3016"){
      input = '<div class="row"><div class="form-group col-md-6"><label>污染物编码：</label>\n' +
        '<input type="text" id="pollutantCode" class="form-control" style="background-color: rgba(72,172,194,0.8);border: 1px solid #41a1b8;" >\n' +
        '</div><div class="form-group col-md-6"><label>采样起始时间：</label>\n' +
        '<input type="text" id="sampTime" class="form-control" style="background-color: rgba(72,172,194,0.8);border: 1px solid #41a1b8;" >\n' +
        '</div></div>' +
        '<div class="row"><div class="form-group col-md-6"><label>采样时间间隔：</label>\n' +
        '<input type="text" id="sampTimeInterval" class="form-control" style="background-color: rgba(72,172,194,0.8);border: 1px solid #41a1b8;" >\n' +
        '</div></div>';
    }else if(code=="30201"){
      input = '<div class="row"><div class="form-group col-md-6"><label>污染物编码：</label>\n' +
        '<input type="text" id="pollutantCode" class="form-control" style="background-color: rgba(72,172,194,0.8);border: 1px solid #41a1b8;" >\n' +
        '</div><div class="form-group col-md-6"><label>现场端信息编码：</label>\n' +
        '<input type="text" id="localInfoCode" class="form-control" style="background-color: rgba(72,172,194,0.8);border: 1px solid #41a1b8;" >\n' +
        '</div></div>'+
        '<div class="row"><div class="form-group col-md-6">\n' +
        '  <label>起始时间：</label>\n' +
        '  <input type="text" class="form-control" id="startTime" style="background-color: rgba(72,172,194,0.8);border: 1px solid #41a1b8;">\n' +
        '</div>\n' +
        '<div class="form-group col-md-6">\n' +
        '  <label>截止时间：</label>\n' +
        '  <input id="endTime" type="text" class="form-control" style="background-color: rgba(72,172,194,0.8);border: 1px solid #41a1b8;">\n' +
        '</div></div>';
    }else if(code=="30202"){
      input = '<div class="row"><div class="form-group col-md-6"><label>污染物编码：</label>\n' +
        '<input type="text" id="pollutantCode" class="form-control" style="background-color: rgba(72,172,194,0.8);border: 1px solid #41a1b8;" >\n' +
        '</div><div class="form-group col-md-6"><label>现场端信息编码：</label>\n' +
        '<input type="text" id="localInfoCode" class="form-control" style="background-color: rgba(72,172,194,0.8);border: 1px solid #41a1b8;" >\n' +
        '</div></div>';
    }else if(code=="30203"){
      input = '<div class="row"><div class="form-group col-md-6"><label>污染物编码：</label>\n' +
        '<input type="text" id="pollutantCode" class="form-control" style="background-color: rgba(72,172,194,0.8);border: 1px solid #41a1b8;" >\n' +
        '</div><div class="form-group col-md-6"><label>现场端信息编码：</label>\n' +
        '<input type="text" id="localInfoCode" class="form-control" style="background-color: rgba(72,172,194,0.8);border: 1px solid #41a1b8;" >\n' +
        '</div></div>';
    }else if(code=="3021"){
      input = '<div class="row"><div class="form-group col-md-6"><label>污染物编码：</label>\n' +
        '<input type="text" id="pollutantCode" class="form-control" style="background-color: rgba(72,172,194,0.8);border: 1px solid #41a1b8;" >\n' +
        '</div><div class="form-group col-md-6"><label>现场端信息编码：</label>\n' +
        '<input type="text" id="localInfoCode" class="form-control" style="background-color: rgba(72,172,194,0.8);border: 1px solid #41a1b8;" >\n' +
        '</div></div>'+
        '<div class="row"><div class="form-group col-md-6"><label>值：</label>\n'+
        '<input type="text" id="infoValue" class="form-control" style="background-color: rgba(72,172,194,0.8);border: 1px solid #41a1b8;" >\n'+
        '</div></div>';
    }else if(code=="4001"){
      input = '<div class="row"><div class="form-group col-md-6"><label>数据间隔(分钟)：</label>\n' +
        '<input type="text" id="interval" class="form-control" style="background-color: rgba(72,172,194,0.8);border: 1px solid #41a1b8;" >\n' +
        '</div></div>';
    }else if((code=="1061")||(code=="1063")||(code=="2011")||(code=="2012")||(code=="2021")||(code=="2022")||(code=="3015")||(code=="4002")){
      input='';
    }
    $('.buildDiv').append(input);
    this.dateRangePicker();
    this.endTimePicker();
  }

  sendMessage(info){
    let cp = new Array<any>();
    // let dacId = "1";
    // console.log(this.dacId);
    let radioCode =$("input[name='CN']:checked").val();
    if(radioCode == undefined){
      alert("请选择反控命令类型！");
      return;
    }else if((radioCode == '1061')||(radioCode=="1063")||(radioCode=="2011")||(radioCode=="2012")||(radioCode=="2021")||(radioCode=="2022")||(radioCode=="3015")||(radioCode=="4002")){

    }else if(radioCode=="1000"){
      cp.push(new keyValue("overTime",$("#timeout").val()));//超时时间
      cp.push(new keyValue("reCount",$("#retransmission").val()))//重发次数
    }else if(radioCode=="1012"){
      cp.push(new keyValue("polId",$("#pollutantCode").val()));//污染物编码
      cp.push(new keyValue("systemTime",$("#startTime").val()))//上位机系统时间
    }else if(radioCode=="1062"){
      cp.push(new keyValue("rtdInterval",$("#realTimeData").val()));//实时数据间隔
    }else if(radioCode=="1064"){
      cp.push(new keyValue("minInterval",$("#minuteData").val()));//分钟数据间隔
    }else if(radioCode=="1072"){
      cp.push(new keyValue("newPw",$("#password").val()));//密码
    }else if((radioCode=="2031")||(radioCode=="2041")||(radioCode=="2051")||(radioCode=="2061")){
      cp.push(new keyValue("beginTime",$("#startTime").val()));//起始时间
      cp.push(new keyValue("endTime",$("#endTime").val()));//截止时间
    }else if((radioCode=="1011")||(radioCode=="3011")||(radioCode=="3012")||(radioCode=="3013")||(radioCode=="3014")||(radioCode=="3017")||(radioCode=="3018")||(radioCode=="3019")||(radioCode=="4003")){
      cp.push(new keyValue("polId",$("#pollutantCode").val()));//污染物编码
    }else if(radioCode=="3016"){
      cp.push(new keyValue("polId",$("#pollutantCode").val()));//污染物编码
      cp.push(new keyValue("cstartTime",$("#sampTime").val()));//采样起始时间
      cp.push(new keyValue("ctime",$("#sampTimeInterval").val()));//采样时间间隔
    }else if(radioCode=="30201"){
      radioCode = "3020"
      cp.push(new keyValue("polId",$("#pollutantCode").val()));//污染物编码
      cp.push(new keyValue("infoId",$("#localInfoCode").val()));//现场端信息编码
      cp.push(new keyValue("beginTime",$("#startTime").val()));//起始时间
      cp.push(new keyValue("endTime",$("#endTime").val()));//截止时间
    }else if(radioCode=="30202"){
      radioCode = "3020";
      cp.push(new keyValue("polId",$("#pollutantCode").val()));//污染物编码
      cp.push(new keyValue("infoId",$("#localInfoCode").val()));//现场端信息编码
    }else if(radioCode=="30203"){
      radioCode = "3020";
      cp.push(new keyValue("polId",$("#pollutantCode").val()));//污染物编码
      cp.push(new keyValue("infoId",$("#localInfoCode").val()));//现场端信息编码
    }else if(radioCode=="3021"){
      cp.push(new keyValue("polId",$("#pollutantCode").val()));//污染物编码
      cp.push(new keyValue("infoId",$("#localInfoCode").val()));//现场端信息编码
      cp.push(new keyValue($("#localInfoCode").val()+"-Info",$("#infoValue").val()));//值
    }else if(radioCode="4001"){
      cp.push(new keyValue("minInterval",$("#interval").val()));//数据间隔(分钟)
    }
    this.message = new ReverseMessage(this.dacId,radioCode,cp);
    this.http.post(this.url+'/reverse/sendMessage',this.message).subscribe(data =>{
    if(data['resultCode']=='0'){
      this.aa(data['extParam']);
    }
    })
  }

  aa(ReverseLogId){
    this.Interval= setInterval(() => {
      if(this.time==10){
        this.http.get(this.url +'/log/queryReverseLogById/'+ReverseLogId).subscribe(data => {
          let status = "";
          if(data['status']=='0'){
            status = "已创建";
          }else if(data['status']=='1'){
            status = "服务端已发送";
          }else if(data['status']=='2'){
            status = "数采仪已接收";
          }else if(data['status']=='3'){
            status = "数采仪已执行";
          }else{

          }
          this.inputList = "<section class=\"content\">\n" +
            "      <div class=\"row\">\n" +
            "        <div class=\"col-md-12 col-sm-12 col-xs-12\">\n" +
            "          <form>\n" +
            "            <div class=\"box-body\">\n" +
            "              <table border=\"1\" cellspacing=\"0\"  class=\"table text-center listTable\">\n" +
            "                <tr >\n" +
            "                  <th>mn</th>\n" +
            "                  <th>qn</th>\n" +
            "                  <th>指令编码</th>\n" +
            "                  <th>消息</th>\n" +
            "                  <th>创建时间</th>\n" +
            "                  <th>发送时间</th>\n" +
            "                  <th>接收时间</th>\n" +
            "                  <th>执行时间</th>\n" +
            "                  <th>修改时间</th>\n" +
            "                  <th>已发次数</th>\n" +
            "                  <th>执行结果</th>\n" +
            "                  <th>状态</th>\n" +
            "                </tr>\n" +
            "                <tr>\n" +
            "                  <td>"+data['mn']+"</td>\n" +
            "                  <td>"+data['qn']+"</td>\n" +
            "                  <td>"+data['cn']+"</td>\n" +
            "                  <td>"+data['message']+"</td>\n" +
            "                  <td>"+data['createTime']+"</td>\n" +
            "                  <td>"+data['sendTime']+"</td>\n" +
            "                  <td>"+data['receiveTime']+"</td>\n" +
            "                  <td>"+data['executeTime']+"</td>\n" +
            "                  <td>"+data['updateTime']+"</td>\n" +
            "                  <td>"+data['sendCount']+"</td>\n" +
            "                  <td>"+data['receiveResult']+"</td>\n" +
            "                  <td>"+ status+" </td>\n" +
            "                </tr>\n" +
            "              </table>\n" +
            "            </div>\n" +
            "          </form>\n" +
            "        </div>\n" +
            "      </div>\n" +
            "    </section>";
          $('.ListDiv').html("");
          $('.ListDiv').append(this.inputList);
        });
      }
      if(this.time<1){
        this.time=11;
      }
      this.time -= 1;
    }, 1000);
  }

  dateRangePicker() {
    let picker: any = $('#startTime');
    let dataRageOption: Object = {
      "singleDatePicker": true,
      "timePicker": true,
      "timePicker24Hour": true,
      "drops":"up",
      "locale": {
        "format": "YYYY-MM-DD HH:mm:ss",
        "applyLabel": "确定",
        "cancelLabel": "取消",
        "customRangeLabel": "Custom",
        "daysOfWeek": ["日", "一", "二", "三", "四", "五", "六"],
        "monthNames": ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"]
      }
    };
    picker.daterangepicker(dataRageOption, function (start, end, label) {
      console.log(`start:${start.format('YYYY-MM-DD HH:mm:ss')}, end:${end}, label:${label}`);
    });
  }
  endTimePicker(){
    let picker:any=$('#endTime');
    let dataRageOption:Object={
      "singleDatePicker":true,
      "timePicker": true,
      "timePicker24Hour": true,
      "drops":"up",
      "locale": {
        "format": "YYYY-MM-DD HH:mm:ss",
        "applyLabel": "确定",
        "cancelLabel": "取消",
        "customRangeLabel": "Custom",
        "daysOfWeek": ["日","一","二","三","四","五","六"],
        "monthNames": ["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"]
      }
    };
    picker.daterangepicker(dataRageOption,function (start,end,label) {
      this.startTime = start.format('YYYY-MM-DD HH:mm:ss');
      console.log(`start:${start.format('YYYY-MM-DD HH:mm:ss')}, end:${end}, label:${label}`);
    });
  }

  // 组件销毁之前
  ngOnDestroy() {//当前页面跳转时，销毁定时任务。
    window.clearInterval(this.Interval);
  }
}

export class ReverseMessage{
  constructor(
    private dacId : string,//数采仪id
    private cn : string,//命令编码
    private cp : Array<Map<any,any>>//指令参数
  ){}
}
export class keyValue{
  constructor(
    private key : string,//数采仪id
    private value : string//命令编码
  ){}
}
